/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/14 22:25
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */
(function () {
    "use strict";

    var api = {
        getTmpSetting: '/goods/express/getSetting',
        setTmpUrl: '/goods/express/setSetting'
    };

    sm.controller('admin-goods-express-controller', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {


        $scope.tmp = {
            payer: "buyer",
            tmpName: "",
            dispatchAddr: ''
        };

        $scope.weightSetting = {
            area: '中国',
            firstWeight: 1,
            firstCost: 0,
            incrWeight: 1,
            incrCost: 0
        };

        $scope.countType = 'weight';

        (function () {

            $http.get(api.getTmpSetting).success(function (res) {
                if (res.state && res.data) {
                    var d = res.data;
                    $scope.tmp.payer = d.payer;
                    $scope.tmp.tmpName = d.tmpName;
                    $scope.tmp.dispatchAddr = d.dispatchAddr;

                    $scope.weightSetting.area = d.area;
                    $scope.weightSetting.firstWeight = d.firstWeight;
                    $scope.weightSetting.firstCost = d.firstCost;
                    $scope.weightSetting.incrWeight = d.incrWeight;
                    $scope.weightSetting.incrCost = d.incrCost;

                    $scope.countType = d.payType;
                }
            });

        })();

        // 设置付款方
        $scope.setPayer = function (type) {

            if (type !== 'buyer' && type !== 'seller') {
                return false;
            }

            $scope.tmp.payer = type;


        };

        $scope.setTmp = function () {

            var t = $scope.tmp;
            if (!t.tmpName || !t.dispatchAddr || !t.payer) {
                Modal.alert('有必填项没有填写');
                return false;
            }

            if (t.payer == 'buyer') {
                var w = $scope.weightSetting;
                if (!w.area || !w.firstWeight || !w.incrWeight) {
                    return false;
                }
            }

            $http.post(api.setTmpUrl, {
                tmpName: t.tmpName,
                payer: t.payer,
                dispatchAddr: t.dispatchAddr,
                count: {
                    'countType': $scope.countType,
                    'lists': [$scope.weightSetting]
                }
            }).success(function (res) {
                if (res.state) {
                    Modal.success('设置成功');
                } else {
                    var err = res.error ? res.error : '设置失败';
                    Modal.error(err);
                }
            });


        };


    }]);

})();
